/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.pig4cloud.pigx.shop.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pigx.common.core.constant.enums.OperEnum;
import com.pig4cloud.pigx.common.core.constant.enums.PointChangeSourceEnum;
import com.pig4cloud.pigx.common.core.constant.enums.PointChangeStatusEnum;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.security.util.SecurityUtils;
import com.pig4cloud.pigx.order.api.entity.OrderInfo;
import com.pig4cloud.pigx.order.api.vo.OrderInfoVO;
import com.pig4cloud.pigx.shop.user.api.entity.ShopUserInfo;
import com.pig4cloud.pigx.shop.user.api.entity.ShopUserPointChangeRecord;
import com.pig4cloud.pigx.shop.user.api.vo.ShopUserPointChangeRecordVO;
import com.pig4cloud.pigx.shop.user.mapper.ShopUserPointChangeRecordMapper;
import com.pig4cloud.pigx.shop.user.service.ShopUserInfoService;
import com.pig4cloud.pigx.shop.user.service.ShopUserPointChangeRecordService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户积分变更记录
 *
 * @author pigx code generator
 * @date 2023-12-23 09:11:16
 */
@Slf4j
@Service
@AllArgsConstructor
public class ShopUserPointChangeRecordServiceImpl
		extends ServiceImpl<ShopUserPointChangeRecordMapper, ShopUserPointChangeRecord>
		implements ShopUserPointChangeRecordService {

	private final ShopUserInfoService shopUserInfoService;

	private final RocketMQTemplate rocketMQTemplate;

	/**
	 * 分页获取积分记录
	 * @param page
	 * @param shopUserPointChangeRecord
	 * @return
	 */
	@Override
	public IPage getUserPointChangePage(Page page, ShopUserPointChangeRecord shopUserPointChangeRecord) {
		IPage<ShopUserPointChangeRecordVO> result = new Page<>();
		LambdaQueryWrapper<ShopUserPointChangeRecord> lqwShopUserPointChangeRecord = new LambdaQueryWrapper<>();
		lqwShopUserPointChangeRecord.eq(ShopUserPointChangeRecord::getShopUserId,
				shopUserPointChangeRecord.getShopUserId());
		lqwShopUserPointChangeRecord.orderByDesc(ShopUserPointChangeRecord::getCreateTime);
		IPage<ShopUserPointChangeRecord> items = this.page(page, lqwShopUserPointChangeRecord);
		if (items.getRecords().size() > 0) {
			result = items.convert(u -> {
				ShopUserPointChangeRecordVO v = new ShopUserPointChangeRecordVO();
				BeanUtils.copyProperties(u, v);
				return v;
			});

			result.getRecords().forEach(item -> {
				if (item.getStatus().equals(PointChangeStatusEnum.PROCESSING.getType())) {
					item.setStatusName("结算中");
				}
				else if (item.getStatus().equals(PointChangeStatusEnum.SUCCESS.getType())) {
					item.setStatusName("已结算");
				}
				else if (item.getStatus().equals(PointChangeStatusEnum.FAIL.getType())) {
					item.setStatusName("结算失败");
				}
			});
		}

		return result;
	}

	/**
	 * 平台修改用户积分
	 * @param shopUserPointChangeRecord
	 * @return
	 */
	@Override
	public R platformUserPointChange(ShopUserPointChangeRecord shopUserPointChangeRecord) {
		if (shopUserPointChangeRecord.getShopUserId() == null) {
			return R.failed("用户ID不能为空");
		}

		Long shopUserId = shopUserPointChangeRecord.getShopUserId();
		ShopUserInfo shopUserInfo = shopUserInfoService
				.getOne(Wrappers.<ShopUserInfo>lambdaQuery().eq(ShopUserInfo::getShopUserId, shopUserId), false);
		if (shopUserInfo == null) {
			return R.failed("当前用户不存在");
		}
		if (shopUserPointChangeRecord.getOper().equals(OperEnum.SUB.getType())) {
			// 减法操作的场合
			if (shopUserInfo.getPointBalance() < shopUserPointChangeRecord.getPoint()) {
				return R.failed("减少的积分不能大于积分余额");
			}
		}
		shopUserPointChangeRecord.setRecordName("平台操作");
		shopUserPointChangeRecord.setStatus(PointChangeStatusEnum.PROCESSING.getType());
		shopUserPointChangeRecord.setRecordType(PointChangeSourceEnum.PLATFORM.getType());
		this.save(shopUserPointChangeRecord);

		Long pointChangeRecordId = shopUserPointChangeRecord.getId();

		// 发送积分变更消息
		log.info("发送平台积分变更消息ID:" + pointChangeRecordId);
		rocketMQTemplate.convertAndSend("shop-user-point-change", pointChangeRecordId.toString());
		log.info("成功发送平台积分变更消息ID:" + pointChangeRecordId);

		return R.ok();
	}

}
